<template>
  <view class="container">
    <z-paging-swiper>
      <view slot="top">
        <u-navbar
          height="50"
          :border-bottom="false"
          title="采购订单"
          title-size="36"
          title-color="#333"
        ></u-navbar>
        <view class="tabsView">
          <u-tabs-swiper
            ref="uTabs"
            :list="tabsList"
            :current="current"
            @change="tabsChange"
            :is-scroll="false"
            :show-bar="false"
            active-color="#10A28F"
            swiper-width="750"
          >
          </u-tabs-swiper>
        </view>
        <u-gap height="20" bg-color="#F0F1F2"></u-gap>
      </view>
      <swiper
        class="swiperView"
        :current="scurrent"
        @transition="transition"
        @animationfinish="animationfinish"
      >
        <swiper-item v-for="(sitem, sindex) in tabsList" class="swiperItem" :key="sindex">
          <z-paging
            ref="paging"
            v-model="itemList[sindex]"
            @query="queryList"
            :fixed="true"
            :auto="false"
            :empty-view-img="`${getImageUrl('empty.png')} `"
            empty-view-text="暂无订单数据~"
            :empty-view-img-style="imgStyle"
          >
            <view class="goodsItemView">
              <view class="itemView" v-for="(item, index1) in itemList[sindex]" :key="index1">
                <view class="order-top dis-flex flex-y-center flex-y-between">
                  <view
                    class="dis-flex flex-y-center"
                    v-if="item.shop"
                    @click.stop.prevent="onShop(item.shop)"
                  >
                    <u-image
                      class="goods-img"
                      style="margin-right: 20rpx"
                      :src="item.picUrl"
                      width="60rpx"
                      height="60rpx"
                      border-radius="30"
                    ></u-image>
                    {{ item.shop.name }}
                  </view>
                  <view class="order-top-tip">
                    {{ item.statusName }}
                  </view>
                </view>
                <scroll-view scroll-x class="scrollView" :enhanced="true" :show-scrollbar="false">
                  <view
                    @click="onOrderDetail(item)"
                    class="order-goods"
                    v-for="item2 in item.items"
                    :key="item2.id"
                  >
                    <u-image
                      class="goods-img"
                      :src="item2.picUrl"
                      width="160rpx"
                      height="160rpx"
                      border-radius="10"
                    ></u-image>
                    <view class="goods-title oneline-hide">
                      {{ item2.spuName }}
                    </view>
                  </view>
                </scroll-view>
                <view class="price-total dis-flex flex-y-center flex-y-between">
                  <view>共{{ item.productCount }}件</view>
                  <view>
                    合计：<text style="color: #fa453c"
                      >¥{{ (item.payPrice * 0.01).toFixed(2) }}</text
                    >
                  </view>
                </view>
                <view class="btn-group" v-if="item.status == 0">
                  <view class="solid-btn btn" @click.stop.prevent="onPay(item)"> 立即支付 </view>
                  <view @click.stop.prevent="onOrderCancel(item)" class="hollow-btn btn">
                    取消订单
                  </view>
                </view>
                <view
                  @click.stop.prevent="onOrderConfrim(item)"
                  class="btn-group"
                  v-else-if="item.status == 20"
                >
                  <view class="solid-btn btn"> 立即收货 </view>
                  <view
                    @click.stop.prevent="onExpress(item)"
                    v-if="item.deliveryType == 1"
                    class="hollow-btn btn"
                  >
                    查看物流
                  </view>
                </view>
                <view
                  class="btn-group"
                  v-else-if="(item.status == 30 || item.status == 10) && item.deliveryType == 1"
                >
                  <view
                    class="solid-btn btn"
                    @click.stop.prevent="toComment(item)"
                    v-if="item.status == 30 && !item.commentStatus"
                  >
                    立即评价
                  </view>
                  <view class="btn hollow-btn" @click.stop.prevent="onExpress(item)">
                    查看物流
                  </view>
                </view>
                <view class="btn-group" v-else-if="item.status == 10 && item.deliveryType == 4">
                  <view @click.stop.prevent="onClerk(item)" class="solid-btn btn"> 去核销 </view>
                </view>
              </view>
            </view>
          </z-paging>
        </swiper-item>
      </swiper>
    </z-paging-swiper>

    <PayPopup :show="show" :payOrderId="payOrderId" :resStatus="resStatus">
      <p style="font-size: 50rpx; font-weight: 600; padding: 20rpx; text-align: center">
        ¥{{ realPay }}
      </p>
    </PayPopup>
    <u-popup
      :closeable="true"
      close-icon-pos="top-left"
      v-model="show2"
      mode="bottom"
      height="auto"
      border-rad
      border-radius="15"
    >
      <view class="popup-box">
        <view class="popup-title"> 发表评论 </view>

        <view class="goods-start">
          <text>商品评价</text>
          <view class="start-bar">
            <u-rate size="50" active-color="#FBC258" v-model="form.scores"></u-rate>
          </view>
        </view>

        <view class="">
          <view class=""> 评论内容 <text style="color: #999999">（至少15字以上）</text> </view>
          <textarea
            v-model="form.content"
            style="width: 100%; height: 120rpx; margin: 15rpx 0; font-size: 28rpx"
            name=""
            placeholder="商品满足您的期待吗？说说它的优点和美中不足吧。"
            id=""
            cols="50"
            rows="20"
          ></textarea>
        </view>

        <view class="image-group">
          <view class="">
            图片<text style="color: #999999">（{{ fileList ? fileList.length : 0 }}/3）</text>
          </view>
          <view class="" style="margin-top: 20rpx">
            <u-upload
              :custom-btn="true"
              :action="action"
              max-count="3"
              ref="uUpload"
              :file-list="fileList"
              :before-upload="beforeFileUpload"
              @on-remove="onFileRemove"
            >
              <view
                slot="addBtn"
                class="slot-btn"
                hover-class="slot-btn__hover"
                hover-stay-time="150"
              >
                <u-icon name="/static/xj.png" size="80"></u-icon>
                <view style="color: #999999">添加照片</view>
              </view>
            </u-upload>
          </view>
        </view>
        <view class="" style="margin-bottom: 100rpx"></view>

        <view class="fixed-wapper">
          <view class="submit-btn" @click="pushComment"> 提交评价 </view>
        </view>
      </view>
    </u-popup>
  </view>
</template>

<script>
import PayPopup from '@/components/PayPopup.vue'
import { BASE_URL, UP_URL } from '@/common/config'
export default {
  components: {
    PayPopup
  },
  data() {
    return {
      action: BASE_URL + UP_URL,
      imgStyle: {
        width: '12rem'
      },
      fileList: [],
      show2: false,
      show: false,
      payOrderId: '',
      current: 0,
      scurrent: 0,
      realPay: '',
      resStatus: '',
      tabsList: [
        {
          name: '全部',
          status: '',
          commentStatus: ''
        },
        {
          name: '待付款',
          key: 'payment',
          count: 0
        },
        {
          name: '待收货',
          iconUrl: '/static/dsh.png',
          key: 'receive',
          count: 0
        },
        {
          name: '已完成',
          iconUrl: '/static/thh.png',
          key: 'complete',
          count: 0
        }
      ],
      itemList: [[], [], [], []],
      form: {
        content: '',
        scores: 5,
        descriptionScores: 5,
        benefitScores: 5,
        orderId: null,
        picUrls: []
      },
      order_id: ''
    }
  },
  onLoad(res) {
    if (res.index) {
      this.current = Number(res.index) ? Number(res.index) : 0
      this.scurrent = Number(res.index) ? Number(res.index) : 0
    }
    if (res.status) {
      let _index = this.tabsList.findIndex((item) => item.key == res.status)
      console.log(_index, res.status)
      this.current = _index ? _index : 0
      this.scurrent = _index ? _index : 0
    }
  },

  onShow() {
    let self = this
    this.queryList()
  },
  methods: {
    tabsChange(index) {
      this.scurrent = index
    },
    transition(e) {
      let dx = e.detail.dx
      this.$refs.uTabs.setDx(dx)
    },
    animationfinish(e) {
      this.current = e.detail.current
      this.scurrent = e.detail.current
      this.$refs.uTabs.setFinishCurrent(this.scurrent)
      if (this.itemList[this.current].length == 0) {
        this.queryList(1)
      }
      // console.log('animationfinish',this.current,this.scurrent)
    },
    queryList(pageNo = 1, pageSize = 10) {
      if (pageNo === 1) {
        this.itemList[this.current] = []
      }
      this.$u
        .get('/app-api/trade/order/page', {
          pageNo,
          pageSize,
          orderStatus: this.tabsList[this.scurrent].key
          // commentStatus: this.tabsList[this.scurrent].commentStatus
        })
        .then((res) => {
          this.$refs.paging[this.scurrent].complete(res.data.list)
        })
    },
    onPay(e) {
      this.payOrderId = e.no
      this.realPay = e.payPrice * 0.01
      // uni.$on("pop", (e) => {
      //   if (e == 2) {
      //     this.queryList();
      //   }
      //   this.show = false;
      // });
      this.resStatus = 2
      this.show = true
    },
    toComment(item) {
      this.form.orderId = item.id
      this.show2 = true
    },
    beforeFileUpload(_index, list) {
      this.fileList = list
    },
    onFileRemove(_index, list) {
      this.fileList = list
    },
    pushComment() {
      let self = this
      self.$refs.uUpload.lists.forEach((val) => {
        self.form.picUrls.push(val.response.data)
      })
      if (self.form.scores == 0) {
        self.$u.toast('评价最低为1分')
        return
      }
      if (self.form.content == '') {
        self.$u.toast('请输入评价内容')
        return
      }
      self.$u.post('/app-api/product/comment/create', self.form).then((res) => {
        if (res.code === 0) {
          self.form.content = ''
          self.form.scores = 5
          self.form.orderId = null
          self.form.picUrls = []
          self.$u.toast('评价成功')
          setTimeout(function () {
            self.show2 = false
          }, 800)
        } else {
          self.$u.toast(res.msg)
        }
      })
    },
    onOrderCancel(item) {
      let self = this
      uni.dialog.confirm('温馨提示', '确定要取消订单吗？', () => {
        self.$u.get('/app-api/trade/order/cancel?id=' + item.id).then((res) => {
          self.$u.toast('取消成功')
          setTimeout(() => {
            this.queryList()
          }, 500)
        })
      })
    },
    onOrderConfrim(item) {
      let self = this
      uni.dialog.confirm('温馨提示', '该订单确认收货吗？', () => {
        // self.$api.putOrderReceive({id:item.id}).then(res => {
        // 	self.$u.toast('收货成功');
        // 	setTimeout(() => {
        // 		this.queryList()
        // 	}, 500)
        // });
        self.$u.post('/app-api/trade/order/receive?id=' + item.id).then((res) => {
          self.$u.toast('收货成功')
          setTimeout(() => {
            this.queryList()
          }, 500)
        })
      })
    },
    onResellPush(item) {
      this.$utils.route({
        url: '/goods/resell-push/resell-push',
        params: {
          id: item.id
        }
      })
    },
    onPushComment(item) {
      this.$utils.route({
        url: '/other/push-comment/push-comment',
        params: {
          id: item.id
        }
      })
    },
    onOrderDetail(item) {
      this.$utils.route({
        url: '/gather/invest/pur-order-detail',
        params: {
          id: item.id
        }
      })
    },
    onExpress(e) {
      uni.navigateTo({
        url:
          '/other/express-process/express-process?id=' +
          e.id +
          '&goodsJson=' +
          JSON.stringify(e.items[0])
      })
    },

    onClerk(item) {
      if (item.qrcode) {
        uni.setStorageSync('verify_qrcode', item.qrcode)
      }
      this.$utils.route('/goods/order-qrcode/order-qrcode?verifyCode=' + item.verifyCode)
    },

    onShop(e) {
      uni.navigateTo({
        url: '/other/merchant-center/merchant-center?id=' + e.id
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import url('@/style/btn.css');

page {
  height: 100%;
  background: #f0f1f2;
}

.tabsView {
  display: flex;
  flex-direction: column;
  background: #ffffff;
  height: 90rpx;
}

.goodsItemView {
  display: flex;
  flex-direction: column;
}

.itemView {
  display: flex;
  flex-direction: column;
  background-color: #ffffff;
  margin-bottom: 1rem;
  border-radius: 6px;
  padding: 20rpx;
}

.order-top {
  position: relative;
  padding-bottom: 20rpx;
  padding-top: 5rpx;
  font-size: 30rpx;
}

.order-top-tip {
  /* 	position: absolute;
		top:15rpx;
		right: 0; */
  color: #999999;
}

.order-goods {
  /* display: flex; */
}

.goods-img {
  border-radius: 20rpx;
}

/* .orde-riget{
		width: 100%;
		margin-left: 10rpx;
		line-height: 55rpx;
		font-size: 26rpx;
	} */

.goods-speci {
  position: relative;
}

.goods-num {
  position: absolute;
  top: 0;
  right: 0;
  color: #999999;
}
.goods-price {
  font-size: 30rpx;
  color: #f54831;
}
.price-total {
  padding: 30rpx 0;
  border-top: #ededed solid 2rpx;
  border-bottom: #ededed solid 2rpx;
  text-align: right;
  font-size: 28rpx;
  margin: 15rpx 0;
}

.btn-group {
  display: flex;
  flex-direction: row-reverse;
  padding: 10rpx 0;
}

.btn {
  display: inline-block;
  padding: 15rpx 20rpx;
  border-radius: 35rpx;
  font-size: 28rpx;
  margin-left: 30rpx;
}
.hollow-btn {
  color: #666666;
  border: #e6e6e6 solid 2rpx;
}
.solid-btn {
  background-color: #10a28f;
  color: #fff;
}

.popup-box {
  padding: 20rpx;
}

.popup-title {
  font-size: 36rpx;
  text-align: center;
}

.goods-start {
  border-top: #ededed solid 2rpx;
  border-bottom: #ededed solid 2rpx;
  padding: 30rpx 0;
  margin: 20rpx 0;
  display: flex;
  align-items: center;
}

.start-bar {
  display: inline-block;
  margin: 0 45rpx;
}

.start {
  margin: 0 13rpx;
}

.image-group {
  /* margin-bottom: 100rpx; */
  padding-bottom: 15rpx;
  border-bottom: #ededed solid 2rpx;
}

.scrollView {
  width: 100%;
  white-space: nowrap;
  box-sizing: border-box;

  .order-goods {
    display: inline-block;
    margin-right: 20rpx;
    width: 160rpx;

    .goods-title {
      width: 100%;
      font-size: 26rpx;
      margin-top: 8rpx;
    }
  }

  .order-goods:last-child {
    margin-right: 0;
  }
}
.slot-btn {
  width: 200rpx;
  height: 200rpx;
  background: #f5f5f5;
  border-radius: 20rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-flow: column;
}
</style>
